package com.lab.utils;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.lab.rtenvironment.AppCoreRuntime;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by ak
 */
public class PermissionUtil {
    public static PermissionClosedInterface mPermissionCloseInterface;

    /**
     * 判断权限是否开启
     *
     * @param permission
     * @return
     */
    public static boolean checkPermission(String permission) {
        try {
            PackageInfo packageInfo = null;
            String permissions[] = null;
            packageInfo = AppCoreRuntime.context.getApplicationContext().getPackageManager().getPackageInfo(AppCoreRuntime.context.getPackageName(), PackageManager.GET_PERMISSIONS);
            permissions = packageInfo.requestedPermissions;
            ArrayList<String> permissionList = new ArrayList<String>(Arrays.asList(permissions));
            return permissionList.contains(permission);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (mPermissionCloseInterface != null) {
            mPermissionCloseInterface.permissionClosed(permission);
        }
        return false;
    }
}
